var cookieDomain = ".xinghaoshangcheng.com";
function updateCartCookie(cartItemsCookie){
	$.cookie('cartItems', cartItemsCookie, { expires: 7, path: '/',domain:cookieDomain}); 
}
function readyDo(){
	//同步本地cookie
	//$.cookie('cartItems',$("#synchronizeCookieStr").val(), { expires: 7, path: '/' }); 
	updateCartCookie($("#synchronizeCookieStr").val());
	$("#address_div").click(function(){
		var token =$.cookie("token");
		var singleBuyFlag =$("#singleBuyFlag").val();
		var url = appPath+"/address/address.html?singleBuyFlag="+singleBuyFlag+"&chooseFlag=1&token="+token;
		self.location=url;
	});
	$("#submit_order").click(function(){
		//$(this).disable();
		var allNoNeedExpress = true;
		var noneedExpressCode = $("#noneedExpressCode").val();
		if(noneedExpressCode!=null&&noneedExpressCode!=''){
			$(".express-select option:selected").each(function(){
				if(noneedExpressCode.indexOf($(this).attr("code"))==-1){
					allNoNeedExpress = false;
				}
			});
		}
		if(!allNoNeedExpress){
			if($("#addressIsNull").val()=="1"){
				$.alert("请添加选择地址！");
				return;
			}
		}
		var pointBuyFlag = $("#pointBuyFlag").val();
		if("1"==pointBuyFlag){
			var userPoints = $("#userPoints").val();
			var consumePoints = $("#consumePoints").val();
			if(parseInt(userPoints)<parseInt(consumePoints)){//用户积分小于要消费的积分
				var msg ='您现有'+parseInt(userPoints)+'积分,所购商品需耗用'+parseInt(consumePoints)+"积分,积分不足,无法购买！";
				$.alert(msg);
				return false;
			}
			$.confirm({
				  title: '积分消费确认',
				  text: '您现有'+parseInt(userPoints)+'积分,提交订单将扣除您个人'+parseInt(consumePoints)+"积分,确认提交？",
				  onOK: function () {
					  dosubmit("1");
				  },
				  onCancel: function () {
					  return false;
				  }
				});
		}else{
			 dosubmit();
		}
	});
}
function dosubmit(pointBuyFlag){
	$.showLoading();
	var url = appPath+"/order/generateOrdersAjax.json"
	var temParam = $("#order_form").serialize();
	//console.log(temParam);
	$("#token-input").val($.cookie("token"));
	//$("#order_form").attr("action",url);
	//$("#order_form").submit();
	$.ajax({
	    type: 'post',
	    url: url,
	    data: $("#order_form").serialize(),
	    success: function(result) {
	    	if("1"==pointBuyFlag){
	    		window.location.href = appPath+"/pay/result.html?result="+result.pointsConsumeResult
	    	}else{
	    		if(result.orderInfo.state==21){//支付成功
	    			window.location.href = appPath+"/pay/result.html?result=true"
	    		}else{
	    			topay(result);
	    		}
	    	}
	    },
	    error:function(msg){
	    	$.hideLoading();
			$.alert("系统异常!");
		}
	});
	
}
function topay(result){
	var orderCode = result.orderInfo.code;
	var synchronizeCookieStr = result.synchronizeCookieStr;
	//更新购物车cookie
	//$.cookie('cartItems', synchronizeCookieStr, { expires: 7, path: '/' }); 
	updateCartCookie(synchronizeCookieStr);
	// your code
	$.ajax({
		url:appPath+"/pay/pay.html",
		dataType:'html',
		type:'post',
		data:{
			token: $.cookie("token"),
			orderCode:orderCode
		},
		success:function(newData){
			$.hideLoading();
			$('body').html(newData);
		},error:function(){
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
}
/**
 * 显示活动内容
 * @param _this
 * @returns {Boolean}
 */
function getActivityContent(activityId,activityType){
	//var activityId =$(_this).val();
	if("-1"==activityId){
		$("#order_gift_div").html("");
		$("#choosed_gift_div").html("");
		$("#giftSkuId").val("");
		return false;
	}
	if(!activityId.match(/^\d+$/)){
		return false;
	}
	$("#activityId").val(activityId);
	if(parseInt(activityType)!=10){//不是满赠，不用去查赠品信息
		$("#order_gift_div").html("");
		return false;
	}
	$.showLoading();
	$.ajax({
		url : appPath+"/activity/getGoodsInfos.html",
		dataType : 'html',
		type : 'post',
		data : {
			activityId : activityId
		},
		success : function(data) {
			$("#order_gift_div").popup();
			$.hideLoading();
			var giftInfoDiv = $(data).find("#order_gift_div");
			$('#order_gift_div').html($(giftInfoDiv[0]).html());
		},
		error : function() {
			$.hideLoading();
			$.alert("系统异常，请重试");
		}
	})
	return true;
}
function getActivityInfo(_this){
	var selVal = $(_this).val();
	var param = selVal.split(",");
	return getActivityContent(param[0],param[1]);
}
/**
 * 显示选中的赠品信息
 */
function showSelectGift(){
	var selectedRadio = $("input[name='giftSkuId']:checked");
	var giftHtml ="";
	var skuId ="";
	if(selectedRadio&&selectedRadio.length>0){
		skuId = $(selectedRadio[0]).val();
		$(selectedRadio[0]).remove();
		giftHtml ='<div class="weui_cells_title " >已选赠品</div>';
		giftHtml +=$("#gift"+skuId).html();
	}
	$("#giftSkuId").val(skuId);
	$("#choosed_gift_div").html(giftHtml);
}
/**
 * 去选择代金券
 */
function chooseVoucher(){
	$("#order_gift_div").html($("#voucher_div").html());
	$("#order_gift_div").popup();
}
/**
 * 返回选择
 */
function returnChoosed(voucherId,voucherPrice){
	$("#voucherId").val(voucherId);
	if(voucherId){
		$("#show_voucher").html("<span style='color:red'>-￥"+formatNumWith2DecimalPlaces(voucherPrice)+"</span>");
	}else{
		$("#show_voucher").html("未使用");
	}
	//$("#show_voucher").html("");
	//$("#show_voucher").html($("#voucher"+voucherId).html());
	$.closePopup();
}

/**
 * 格式化数字
 */
function formatNumWith2DecimalPlaces(num){
	var str = num+"";
	if(str.match(/^\d{1,2}$/g)){
		str="00"+str;
	}
	str = str.substr(0,str.length-2)+"."+str.substr(str.length-2,2);
	return str.replace(/^0{2,}\./g,"0.");
}
function giftCheck(_this){
	var checkedDom = $("input[name='giftSkuId']:checked");
	var checkDom = $(_this).find(":checkbox");
	if(checkedDom.length==1){
		if(!checkDom.prop("checked")){
			$.alert("只能选择一件商品！");
			return false;
		}
	}
	checkDom.prop("checked",!checkDom.prop("checked"));
}
function proventParentEvent(evt){
	 evt.stopPropagation(); 
}